//
//  BookingDetailsViewController.m
//  HondaPro_2
//
//  Created by ht on 13-6-24.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "BookingDetailsViewController.h"
#import "webConfig.h"
#import "webService.h"
#import "NewPopViewController.h"

@interface BookingDetailsViewController ()

@end

@implementation BookingDetailsViewController

@synthesize detailsDictionary = _detailsDictionary;
@synthesize detailsArray = _detailsArray;
@synthesize detailsArrayTow = _detailsArrayTow;

- (id)init
{
    self = [super init];
    if (self)
    {
        self.isShow= YES;
        [self addNotification];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setFrame:CGRectMake(0, 0, 700, 620)];
    
    
    [self loadtitleLabel];
//    self.weiXiuTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(450, 425, 180, 50)];
//    [self.weiXiuTypeLabel setBackgroundColor:[UIColor clearColor]];
//    self.weiXiuTypeLabel.text = wXiuTypeXianShi;
//    self.weiXiuTypeLabel.textColor = [UIColor grayColor];
//    self.weiXiuTypeLabel.font = [UIFont fontWithName:@"ArialMT" size:13];
//    self.weiXiuTypeLabel.lineBreakMode = NSLineBreakByWordWrapping;
//    self.weiXiuTypeLabel.numberOfLines = 0;
//    //[self.view addSubview:self.weiXiuTypeLabel];
//    
//    self.weiXiuGongWei = [[UILabel alloc]initWithFrame:CGRectMake(450, 380, 180, 40)];
//    [self.weiXiuGongWei setBackgroundColor:[UIColor clearColor]];
//    self.weiXiuGongWei.text = self.weiXiuGongWeiName;
//    self.weiXiuGongWei.textColor = [UIColor grayColor];
//    self.weiXiuGongWei.font = [UIFont fontWithName:@"ArialMT" size:14];
//    //[self.view addSubview:self.weiXiuGongWei];
    
}

- (void)setUpData
{
    NSArray *modelArr = [self readershuDICimform][@"MODEL"];
    NSArray *seriesArr = [self readershuDICimform][@"SERIES"];
    NSArray *laiyuanArr = [self readershuDICimform][@"BOOKING_SOURCE"];
    NSArray *yuyueleixingArr = [self readershuDICimform][@"TM_BOOKING_TYPE"];
    NSArray *guwenArr = [self readershuDICimform][@"SERVICE_ADVISOR"];
    NSArray * dengjiArr = [self readershuDICimform] [@"TM_EMPLOYEE"];
    NSString *seriesStr;
    NSString *modelStr;
    NSString *laiyuanStr;
    NSString *yuyueleixingStr;
    NSString *guwenStr;
    NSString * dengjiStr;
    for (NSDictionary * dic in dengjiArr) {
        if ([dic[@"CODE"] isEqualToString:_detailsDictionary[@"OPERATOR"]]) {
            dengjiStr = dic[@"NAME"];
         
        }
    }
    
    for (NSDictionary *dic in modelArr) {
        if ([dic[@"MODEL_CODE"] isEqualToString:_detailsDictionary[@"MODEL"]]) {
            modelStr = dic[@"MODEL_NAME"];
        }
    }
    
    for (NSDictionary *dic in seriesArr) {
        if ([dic[@"SERIES_CODE"] isEqualToString:_detailsDictionary[@"SERIES"]]) {
            seriesStr = dic[@"SERIES_NAME"];
        }
    }
    
    NSNumber *laiyuanNumber = _detailsDictionary[@"RECRUIT_TYPE_NAME"];
    for (NSDictionary *dic in laiyuanArr) {
        if ([dic[@"CODE"] isEqualToString:laiyuanNumber.stringValue]) {
            laiyuanStr = dic[@"NAME"];
        }
    }
    
    for (NSDictionary *dic in yuyueleixingArr) {
        if ([dic[@"BookingTypeCode"] isEqualToString:_detailsDictionary[@"BOOKING_TYPE_CODE"]]) {
            yuyueleixingStr = dic[@"BookingTypeName"];
        }
    }
    
    for (NSDictionary *dic in guwenArr) {
        if ([dic[@"CODE"] isEqualToString:_detailsDictionary[@"SA_NAME"]]) {
            guwenStr = dic[@"NAME"];
        }
    }
    
    self.detailsArray = [NSMutableArray arrayWithCapacity:5];
    self.detailsArrayTow = [NSMutableArray arrayWithCapacity:5];
    [self.detailsArray setArray:@[[_detailsDictionary valueForKey:@"VIN"]?[_detailsDictionary valueForKey:@"VIN"]:@"",[_detailsDictionary valueForKey:@"MOBILE"]?[_detailsDictionary valueForKey:@"MOBILE"]:@"",[_detailsDictionary valueForKey:@"PRECONTRACT_ID"]?[_detailsDictionary valueForKey:@"PRECONTRACT_ID"]:@"",dengjiStr?dengjiStr:@"",[_detailsDictionary valueForKey:@"CUSTOMER_NAME"]?[_detailsDictionary valueForKey:@"CUSTOMER_NAME"]:@"",[_detailsDictionary valueForKey:@"START_TIME"]?[_detailsDictionary valueForKey:@"START_TIME"]:@"",guwenStr?guwenStr:@"",[_detailsDictionary valueForKey:@"CUSTOMER_DESCRIPTION"]?[_detailsDictionary valueForKey:@"CUSTOMER_DESCRIPTION"]:@"",]];
//    [_detailsDictionary valueForKey:@"OPERATOR"]?:@"",
//    dengjiStr?:@"",
 
    
    //self.detailsArray = [[NSMutableArray alloc] initWithObjects:[_detailsDictionary valueForKey:@"VIN"],[_detailsDictionary valueForKey:@"MOBILE"],[_detailsDictionary valueForKey:@"PRECONTRACT_ID"],[_detailsDictionary valueForKey:@"OPERATOR"],[_detailsDictionary valueForKey:@"CUSTOMER_NAME"],[_detailsDictionary valueForKey:@"START_TIME"],guwenStr,nil];
    
    
    [self.detailsArrayTow setArray:@[[_detailsDictionary valueForKey:@"PLATE_NUMBER"]?:@"",[_detailsDictionary valueForKey:@"END_TIME"]?:@"",yuyueleixingStr?:@"",seriesStr?:@"",modelStr?:@"",[_detailsDictionary valueForKey:@"REPAIR_TYPE"]?:@"",laiyuanStr?:@""]];
    //self.detailsArrayTow = [[NSMutableArray alloc]initWithObjects:[_detailsDictionary valueForKey:@"PLATE_NUMBER"],[_detailsDictionary valueForKey:@"END_TIME"],yuyueleixingStr,seriesStr,modelStr,[_detailsDictionary valueForKey:@"CUSTOMER_DESCRIPTION"],laiyuanStr,nil];
    
    NSString *wXiuType = [_detailsDictionary valueForKey:@"repairTypeName"];
    NSString *wXiuTypeXianShi = @"";
    if([wXiuType rangeOfString:@"1"].length == 1)
    {
        wXiuTypeXianShi = [wXiuTypeXianShi stringByAppendingString:@"首保   "];
    }
    if([wXiuType rangeOfString:@"3"].length == 1)
    {
        wXiuTypeXianShi = [wXiuTypeXianShi stringByAppendingString:@"年检保养   "];
    }
    if([wXiuType rangeOfString:@"5"].length == 1)
    {
        wXiuTypeXianShi = [wXiuTypeXianShi stringByAppendingString:@"钣金   "];
    }
    if([wXiuType rangeOfString:@"7"].length == 1)
    {
        wXiuTypeXianShi = [wXiuTypeXianShi stringByAppendingString:@"精品   "];
    }
    if([wXiuType rangeOfString:@"2"].length == 1)
    {
        wXiuTypeXianShi = [wXiuTypeXianShi stringByAppendingString:@"定期保养   "];
    }
    if([wXiuType rangeOfString:@"4"].length == 1)
    {
        wXiuTypeXianShi = [wXiuTypeXianShi stringByAppendingString:@"一般修理   "];
    }
    if([wXiuType rangeOfString:@"6"].length == 1)
    {
        wXiuTypeXianShi = [wXiuTypeXianShi stringByAppendingString:@"喷漆   "];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadBitchLabel];
    [self loadtitleLabel];
}

-(void)getworkStationList2:(NSNotification*)notification
{

    self.stationJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notification.object options:NSJSONReadingMutableLeaves error:Nil];

    NSMutableArray *iDArray = [[NSMutableArray alloc]init];
    NSMutableArray *nMArray = [[NSMutableArray alloc]init];
    for(int i=0;i<[[self.stationJson objectForKey:@"workStationList"]count];i++)
    {
        [iDArray addObject:[[[self.stationJson objectForKey:@"workStationList"]objectAtIndex:i] objectForKey:@"workStationId"]];
        [nMArray addObject:[[[self.stationJson objectForKey:@"workStationList"] objectAtIndex:i] objectForKey:@"workStationName"]];
    }
    NSString *mingzi = [_detailsDictionary objectForKey:@"workStationName"];//从详细信息里取出工位名字
    self.weiXiuGongWeiName = [nMArray objectAtIndex:[iDArray indexOfObject:mingzi]];
    self.weiXiuGongWei.text = self.weiXiuGongWeiName;
}
//服务顾问
-(void)getSaName:(NSNotification*)notification
{
    NSDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notification.object options:NSJSONReadingMutableLeaves error:Nil];
    NSMutableArray *saNameArray = [[NSMutableArray alloc]init];
    NSMutableArray *saIdArray = [[NSMutableArray alloc]init];

    for (int i=0; i<[[theJson objectForKey:@"results"] count]; i++)
    {
        [saNameArray addObject:[[[theJson objectForKey:@"results"]objectAtIndex:i] objectForKey:@"saName"]];
        [saIdArray addObject:[[[theJson objectForKey:@"results"]objectAtIndex:i] objectForKey:@"saID"]];
    }
    NSString *saName = [_detailsDictionary objectForKey:@"saName"];
   
    if (![saName isEqualToString:@""]) {
         self.saId = [saIdArray objectAtIndex:[saNameArray indexOfObject:saName]];
    }
   
//    NSLog(@"服务顾问iD：%@",self.saId);
}

-(void)loadtitleLabel
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(290, 20, 200, 40)];
    label.text = @"预约详情";
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor grayColor];
    label.font = [UIFont fontWithName:@"ArialMT" size:22];
    [self.view addSubview:label];
    
    UIImageView *imageline= [[UIImageView alloc]initWithFrame:CGRectMake(15, 60, 670, 2)];
    imageline.image = [UIImage imageNamed:@"红色分割线.png"];
    [self.view addSubview:imageline];
    
    NSMutableArray *labelMutabelArray = [[NSMutableArray alloc]initWithObjects:@"车  牌  号:",@"登记时间:",@"预约类型:",@"车系:",@"车型:",@"维修分类",@"来源:", nil];
    for(int i = 0; i < labelMutabelArray.count;i++)
    {
        UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(350, 80+i*50, 100, 40)];
        [titlelabel setBackgroundColor:[UIColor clearColor]];
        titlelabel.text = [labelMutabelArray objectAtIndex:i];
        titlelabel.textColor = [UIColor grayColor];
        titlelabel.font = [UIFont fontWithName:@"ArialMT" size:16];
        [titlelabel setTextAlignment:NSTextAlignmentCenter];
        [self.view addSubview:titlelabel];
    }
    
    NSMutableArray *labelMutabelArray1 = [[NSMutableArray alloc]initWithObjects:@"车  架  号:",@"联系电话:",@"预约单号:",@"登  记  人:",@"客户名称:",@"预约时间:",@"服务顾问:",@"备注:", nil];
    for(int i = 0; i < labelMutabelArray1.count;i++)
    {
        UILabel *titlelabel1 = [[UILabel alloc]initWithFrame:CGRectMake(50, 80+i*50, 100, 40)];
        [titlelabel1 setBackgroundColor:[UIColor clearColor]];
        titlelabel1.text = [labelMutabelArray1 objectAtIndex:i];
        titlelabel1.textColor = [UIColor grayColor];
        titlelabel1.font = [UIFont fontWithName:@"ArialMT" size:16];
        [titlelabel1 setTextAlignment:NSTextAlignmentCenter];
        [self.view addSubview:titlelabel1];
    }
    self.ModifyButton = [[UIButton alloc]initWithFrame:CGRectMake(590, 533, 75, 30)];
    [self.ModifyButton setTitle:@"修  改" forState:UIControlStateNormal];
    [self.ModifyButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.ModifyButton setBackgroundImage:[UIImage imageNamed:@"改-灰but.png"] forState:UIControlStateNormal];
    [self.ModifyButton addTarget:self action:@selector(ModifyButton:) forControlEvents:UIControlEventTouchUpInside];
    self.ModifyButton.titleLabel.font = [UIFont fontWithName:@"ArialMT" size:14];
    [self.view addSubview:self.ModifyButton];
    
//    self.laiChangButton = [[UIButton alloc]initWithFrame:CGRectMake(500, 533, 73, 29)];
//    [self.laiChangButton setTitle:@"转接待" forState:UIControlStateNormal];
//    [self.laiChangButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [self.laiChangButton setBackgroundImage:[UIImage imageNamed:@"ht05-1.png"] forState:UIControlStateNormal];
//    [self.laiChangButton addTarget:self action:@selector(laiChangButton:) forControlEvents:UIControlEventTouchUpInside];
//    self.laiChangButton.titleLabel.font = [UIFont fontWithName:@"ArialMT" size:14];
//    [self.view addSubview:self.laiChangButton];

    if (self.isShow) {
        [self.ModifyButton setHidden:NO];
        [self.laiChangButton setHidden:NO];
    }else{
        [self.ModifyButton setHidden:YES];
        [self.laiChangButton setHidden:YES];

    }
}

-(NSMutableDictionary *)readershuDICimform
{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *str=[path stringByAppendingPathComponent:@"Dic.text"];
    dic=[NSMutableDictionary dictionaryWithContentsOfFile:str];
    //nslog(@"%@",dic);
    return dic;
}

#pragma mark 创建客户信息子视图
-(void)addSubView
{
//    NewPopViewController *edit = [[NewPopViewController alloc] init];
//    edit.editdDataDic = self.detailsDictionary;
//    
//    [self dismissWithCompletion:nil];
//    [self presentView:edit.view completion:^{
//        
//    }];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"comeEdit" object:self.detailsDictionary];
}

#pragma mark - 修改按钮的回调
-(void)ModifyButton:(id)sender
{
    [self addSubView];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark - 转来厂按钮的回调
-(void)laiChangButton:(id)sender
{
    [self dismissWithCompletion:nil];
    NSNotification *notification = [NSNotification notificationWithName:@"comeFactory" object:_detailsDictionary];
    [[NSNotificationCenter defaultCenter]postNotification:notification];
}
-(void)loadBitchLabel
{
    [self setUpData];
    for(int i = 0; i < self.detailsArray.count;i++)
    {
        UILabel *titlelabel2 = [[UILabel alloc]initWithFrame:CGRectMake(150, 80+i*50, 160, 40)];
        [titlelabel2 setBackgroundColor:[UIColor clearColor]];
        titlelabel2.text = [self.detailsArray objectAtIndex:i];
        titlelabel2.textColor = [UIColor grayColor];
        titlelabel2.font = [UIFont fontWithName:@"ArialMT" size:14];
        [self.view addSubview:titlelabel2];
    }
    for(int i = 0; i < self.detailsArrayTow.count;i++)
    {
        UILabel *titlelabel3 = [[UILabel alloc]initWithFrame:CGRectMake(450, 80+i*50, 180, 40)];
        [titlelabel3 setBackgroundColor:[UIColor clearColor]];
        titlelabel3.text = self.detailsArrayTow[i];
        titlelabel3.textColor = [UIColor grayColor];
        titlelabel3.font = [UIFont fontWithName:@"ArialMT" size:14];
        [self.view addSubview:titlelabel3];
    }
}

-(void)addNotification
{
    //工位
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getworkStationList2:) name:GETWORKSTATIONLIST object:Nil];
   //服务顾问
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSaName:) name:GETSANAMELIST object:Nil];
}

-(void)removeNotification
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [self.modifyAndAddSubView removeNotification];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
